package com.pets.springboot_petshop.service.impl;

import com.pets.springboot_petshop.entity.Admin;
import com.pets.springboot_petshop.entity.User;
import com.pets.springboot_petshop.entity.UserDTO;
import com.pets.springboot_petshop.mapper.UserMapper;
import com.pets.springboot_petshop.service.UserService;
import com.pets.springboot_petshop.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User select(int id) {
        return userMapper.select(id);
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public User findByUseremail(String email) {
        return userMapper.findByUseremail(email);
    }

    @Override
    public int selectusername(String username) {
        return userMapper.selectusername(username);
    }

    @Override
    public List<User> selectName(String username) {
        return userMapper.selectName(username);
    }

    @Override
    public List<User> selectemail(String email) {
        return userMapper.selectemail(email);
    }

    @Override
    public void addadmin(String username, String phone, String email) {
        userMapper.addadmin(username, phone, email);
    }


    @Override
    public void addUser(String username, String password, String email) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        userMapper.addUser(username, md5String, email);
    }

    @Transactional
    public boolean updateUserpassword(User user) {
        if (user == null || user.getEmail() == null || user.getPassword() == null) {
            System.out.println("密码修改成功2:  " +user);
            return false;
        }
        String encryptedPassword = Md5Util.getMD5String(user.getPassword());
        user.setPassword(encryptedPassword);
        return userMapper.updateUserpassword(user) > 0;
    }

    @Override
    public int updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUserById(int id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public List<User> UserfindAll() {
        return userMapper.UserfindAll();
    }


    @Override
    public UserDTO convertToUserDTO(User user) {
        if (user == null) {
            return null;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        userDTO.setUsername(user.getUsername());
        userDTO.setPhone(user.getPhone());
        userDTO.setEmail(user.getEmail());
        userDTO.setIntegral(user.getIntegral());
        userDTO.setMembership_id(user.getMembership_id());
        userDTO.setCreate_time(user.getCreate_time());
        userDTO.setUpdate_time(user.getUpdate_time());
        userDTO.setImg(user.getImg());
        userDTO.setName(user.getName());
        userDTO.setDiscount(user.getDiscount());
        return userDTO;
    }

    @Override
    public int updateimg(User user) {
        return userMapper.updateimg(user);
    }

    @Override
    public int updateusername(User user) {
        return userMapper.updateusername(user);
    }

    @Override
    public int updatephone(User user) {
        return userMapper.updatephone(user);
    }

    @Override
    public int updatemail(User user) {
        return userMapper.updatemail(user);
    }

    @Override
    public int admindelete(int[] ids) {
        return userMapper.admindelete(ids);
    }
}
